import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_plugin/examples/advanced/stream_message/stream_message.dart';
import 'package:flutter_plugin/examples/basic/join_channel_video/join_channel_video.dart';
import 'package:flutter_plugin/examples/log_sink.dart';
import 'dart:convert' as convert;

import 'package:flutter_plugin/homePage.dart';
import 'package:flutter_plugin/meeting.dart';
import 'package:flutter_plugin/pageOne.dart';

void main() {
  ///这里的CustomFlutterBinding调用务必不可缺少，用于控制Boost状态的resume和pause
  CustomFlutterBinding();
  runApp(MaterialApp(
    home: MyApp(),
    theme: ThemeData(
        appBarTheme:
            const AppBarTheme(systemOverlayStyle: SystemUiOverlayStyle.dark)),
  ));
}

///创建一个自定义的Binding，继承和with的关系如下，里面什么都不用写
class CustomFlutterBinding extends WidgetsFlutterBinding
    with BoostFlutterBinding {}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static Map<String, FlutterBoostRouteFactory> routerMap = {
    ///默认页
    '/': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings, pageBuilder: (_, __, ___) => Meeting());
    },
    ///可以在native层通过 getContainerParams 来传递参数
    'flutterPage': (settings, uniqueId) {
      print('flutterPage settings:$settings, uniqueId:$uniqueId');
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => MyHomePage(
                // params: convert.jsonDecode(settings.arguments.toString()) ,
                params: (settings.arguments ?? {}) as Map<dynamic, dynamic>,
                uniqueId: "$uniqueId",
              ));
    },
    'pageOne': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => PageOne(
                // params: convert.jsonDecode(settings.arguments.toString()) ,
                params: (settings.arguments ?? {}) as Map<dynamic, dynamic>,
                uniqueId: "$uniqueId",
              ));
    },
    'meeting': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings, pageBuilder: (_, __, ___) => Meeting());
    },
    'JoinChannelVideo': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => Scaffold(
                appBar: AppBar(
                  title: const Text("JoinChannelVideo"),
                  actions: const [LogActionWidget()],
                ),
                body: JoinChannelVideo(),
              ));
    },
    'StreamMessage': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => Scaffold(
                appBar: AppBar(
                  title: const Text("StreamMessage"),
                  actions: const [LogActionWidget()],
                ),
                body: StreamMessage(),
              ));
    }
  };

  @override
  void initState() {
    super.initState();
  }

  Route<dynamic>? routeFactory(RouteSettings settings, String? uniqueId) {
    FlutterBoostRouteFactory? func = routerMap[settings.name!];
    print("======================= $settings ====== $uniqueId");
    if (func == null) {
      return null;
    }
    return func(settings, uniqueId);
  }

  @override
  Widget build(BuildContext context) {
    return FlutterBoostApp(routeFactory, interceptors: const []);
  }
}
